package xin.cms.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.cms.model.News;
import xin.cms.repository.NewsRepository;
import xin.cms.repository.CommentRepository;
import xin.cms.request.NewsPageRequest;
import xin.common.date.DateUtils;
import xin.common.http.response.PageResponse;
import xin.common.http.response.PageWrapper;
import xin.common.utils.StringUtils;
import xin.core.ErrorConstant;
import xin.core.exception.SysException;
import xin.core.jpa.JpaColnumConstant;
import xin.core.jpa.JpaConstant;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <pre>
 * 新闻服务
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/8/10
 */
@Service
@Slf4j
@Transactional(readOnly = true)
public class NewsService {

    @Autowired
    private NewsRepository repository;

    @Autowired
    private CommentRepository commentRepository;

    public News findOne(String newsId){
        return  repository.findOne(newsId);
    }

    /**
     * 客户端查看新闻时调用
     * @param newsId  内容ID
     * @return
     */
    @Transactional
    public News readNews(String newsId){
        News News = repository.findOne(newsId);
        if(News != null){
            Integer readTimes = News.getReadTimes();
            if(readTimes == null)
                readTimes = new Integer(0);
            News.setReadTimes(++readTimes);//记录访问量
            return repository.save(News);
        }else{
            throw new SysException(ErrorConstant.ERROR_DATA_0001,"查看内容已不存在");
        }
    }

    public PageResponse findPageList(NewsPageRequest request ){
        Sort sort = new Sort(Sort.Direction.DESC,JpaColnumConstant.STATUS,JpaColnumConstant.LAST_MODIFY_TIME);
        Pageable pageable = new PageRequest(request.getPageNumber() - 1,request.getPageSize(),sort);
        Specification<News> specification = (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if(StringUtils.isNotBlank(request.getTitle())){
                predicates.add(criteriaBuilder.like(root.get("title"),
                        JpaConstant.WILDCARD + request.getTitle() + JpaConstant.WILDCARD));
            }
            if(request.getStatus() !=  null){
                predicates.add(criteriaBuilder.equal(root.get("status"),request.getStatus()));
            }
            if(request.getForum() != null){
                predicates.add(criteriaBuilder.equal(root.get("forum"),request.getForum()));
            }
            if(StringUtils.isNotBlank(request.getCreateTime())){
                Date start = DateUtils.parseDate(request.getCreateTime() + " 00:00:00");
                Date end = DateUtils.addDays(start,1);
                predicates.add(criteriaBuilder.between(root.get(JpaColnumConstant.CREATE_TIME),start,end));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return PageWrapper.wrap(repository.findAll(specification, pageable));
    }

    @Transactional
    public News save(News News){
        return  repository.save(News);
    }
}
